package com.applink.sell_proof;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.Window;

import com.applink.sell_proof.broadcast_receiver.NetworkConnectivityReceiver;
import com.applink.sell_proof.constant.Constant;
import com.applink.sell_proof.dialog.EAlertDialog;
import com.applink.sell_proof.dialog.WaitingDialog;
import com.applink.sell_proof.util.Utility;
import com.google.android.gcm.GCMRegistrar;

public class Splash extends FragmentActivity {

	private static final int LOAD_DATA_SYNC_ONLINE = 0;
	public static final int LOAD_DATA_SYNC_OFFLINE = 1;
	private Intent intent;
	public static Splash getInstant;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.splash);
		getInstant = this;
		// new loadData().execute();
		// initView();
		passToLogin();
	}

	private class loadData extends AsyncTask<Void, Void, Integer> {

		@Override
		protected Integer doInBackground(Void... params) {
			Utility.SyncData();
			return 0;
		}

	}

	private void initView() {
		try {
			Utility.checkNetwork(this);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (EMarketing.isOnline()) {
			mHandler.sendEmptyMessage(LOAD_DATA_SYNC_ONLINE);
		}
	}

	// @Override
	// protected void onResume() {
	// super.onResume();
	// if (!EMarketing.isOnline() && Constant.NETWORK_STATUS == true) {
	// passToLogin();
	// Constant.NETWORK_STATUS = false;
	// }
	// }

	public void showAlertDialog(DialogInterface.OnClickListener listener,
			String title, String navigative, String positive, String message,
			boolean cancelable) {
		WaitingDialog waitingDialog = (WaitingDialog) getSupportFragmentManager()
				.findFragmentByTag(WaitingDialog.TAG);
		if (waitingDialog != null && waitingDialog.isFullScreen()) {
			waitingDialog.dismiss();
		}
		DialogFragment dialogFragment = new EAlertDialog(listener, title,
				navigative, message);
		dialogFragment.setCancelable(cancelable);
		dialogFragment.show(getSupportFragmentManager(), null);
	}

	public Handler mHandler = new Handler(new Handler.Callback() {

		@Override
		public boolean handleMessage(Message msg) {
			switch (msg.what) {
			case LOAD_DATA_SYNC_ONLINE:
				loadDataSync(true);
				break;

			case LOAD_DATA_SYNC_OFFLINE:
				loadDataSync(false);
				break;

			default:
				break;
			}
			return false;
		}
	});

	public void passToLogin() {

		intent = new Intent(getApplicationContext(), NewLogInActivity.class);
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				startActivity(intent);
				finish();
				// DialogFragment login = new LogIn();
				// login.show(getSupportFragmentManager(), LogIn.TAG);
			}
		}, 500);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_HOME);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			finish();
			return true;
		}
		return super.onKeyUp(keyCode, event);
	}

	private void loadDataSync(final boolean checkLogin) {
		new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected Boolean doInBackground(Void... params) {
				final String regId = GCMRegistrar
						.getRegistrationId(getApplicationContext());

				// Automatically registers application on startup.
				if (regId.equals("") && EMarketing.getInstance().isLogin()) {
					GCMRegistrar.register(getApplicationContext(),
							Config.GCM_SENDER_ID);
				}

				return EMarketing.getInstance().isLogin();
			}

			@Override
			protected void onPostExecute(Boolean isLogin) {
				if (isLogin) {

					// log that user is log in
					// AnalyticDBHelper.tracked("Logged in");

					// int currentTab = CustomerSupportHelper
					// .getInt(Constant.DEFAULT_TAB);
					int currentTab = 2;
					switch (currentTab) {
					case 0:
						intent = new Intent(getApplicationContext(),
								ClaimActivity.class);
						break;
					case 1:
						intent = new Intent(getApplicationContext(),
								RewardActivity.class);
						break;
					case 2:
						intent = new Intent(getApplicationContext(),
								LearnActivity.class);
						break;
					case 3:
						intent = new Intent(getApplicationContext(),
								LifeStyleActivity.class);
						break;
					case 4:
						intent = new Intent(getApplicationContext(),
								CustomerServiceActivity.class);
						break;
					default:
						intent = new Intent(getApplicationContext(),
								ClaimActivity.class);
						break;
					}

					new Handler().postDelayed(new Runnable() {

						@Override
						public void run() {
							startActivity(intent);
							finish();
						}
					}, 500);
					// Utility.SyncData();
					NetworkConnectivityReceiver.checkDataSync = true;
				} else {
					// if (checkLogin) {
					passToLogin();
					// }

				}

			}
		}.execute((Void) null);
	}

}
